/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.servlet;

import com.hvtt.web.controller.Sys_NguoiDungController;
import com.hvtt.web.models.Sys_nguoidung;
import com.hvtt.web.utilities.SocialAPIWrapper;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.primefaces.json.JSONException;

/**
 *
 * @author Administrator
 */
public class FacebookServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String code=request.getParameter("code");
            SocialAPIWrapper socialAPIWrapper=new SocialAPIWrapper();
            String accessToken=socialAPIWrapper.getAccessToken(code);
            socialAPIWrapper.setAccessToken(accessToken);
            JSONObject obj=(JSONObject)socialAPIWrapper.getUserInfo();
//            Facebook facebook = new FacebookFactory().getInstance();
//            facebook.setOAuthAppId("748155828562101", "2ffa9772c8491b05c25e3f08cf8ba14f");
//            facebook.setOAuthAccessToken(new AccessToken(socialAPIWrapper.getAccessToken()));
//            RawAPIResponse res = facebook.callGetAPI("me");
//            facebook4j.internal.org.json.JSONObject jsonObject = res.asJSONObject();
//            String id = jsonObject.getString("id");
//            facebook.getAccounts();
            
//            StringBuilder builder=new StringBuilder();
//            builder.append(u.getFirstName()).append(",");
//            builder.append(u.getLastName()).append(",");
//            builder.append(u.getEmail()).append(",");
//            builder.append(u.getGender()).append(",");
//            builder.append(u.getUsername()).append(",");
            
//            Sys_nguoidung sys_nguoidung= Utilities.getManagedBean(Sys_NguoiDungController.class).getItem();
//            sys_nguoidung.setHo(String.valueOf(obj.get("first_name")));
//            sys_nguoidung.setTen(String.valueOf(obj.get("last_name")));
//            sys_nguoidung.setGioiTinh(String.valueOf(String.valueOf(obj.get("gender")).charAt(0)).toUpperCase());
//            
            
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet FacebookServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet FacebookServlet at " + Utilities.getManagedBean(Sys_NguoiDungController.class)+"</h1>");
            out.println("<a href='http://sagoauto.hvttgroup.com/dang-ky.html'>Dang ky</a>");
            out.println("</body>");
            out.println("</html>");
            //response.sendRedirect("/dang-ky.html");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
